package com.neutech.studentgrade.web;

import com.neutech.studentgrade.dao.GradeDAO;
import com.neutech.studentgrade.dao.StudentDAO;
import com.neutech.studentgrade.dao.impl.GradeDAOImpl;
import com.neutech.studentgrade.dao.impl.StudentDAOImpl;
import com.neutech.studentgrade.entity.Grade;
import com.neutech.studentgrade.entity.Student;
import com.neutech.studentgrade.util.ServletUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@WebServlet("*.student")
public class StudentServlet extends HttpServlet {
    private StudentDAO dao = new StudentDAOImpl();
    private GradeDAO dao1 = new GradeDAOImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletUtil.characterEncoding(request, response);
        String url = request.getRequestURL().toString();
        String path = url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));
//        System.out.println("1");
        if ("/findAll".equals(path)) {
//            System.out.println("进来了");
            List<Student> students = dao.findAll();//找到所有
            request.setAttribute("students", students);//绑定数据
            request.getRequestDispatcher("student_list.jsp").forward(request, response);//转发到jsp页面
        }else if ("/findAllRankBySno".equals(path)){
//            System.out.println("查看排名");
            List<Student> students = dao.findAllRankBySno();
            request.setAttribute("student",students);
            request.getRequestDispatcher("studentRank_list.jsp").forward(request,response);
        }else if ("/findAllAvgBySno".equals(path)){
            System.out.println("低于平均分");
            List<Student> students = dao.findAllAvgBySno();
            request.setAttribute("studentAvg",students);
            request.getRequestDispatcher("studentAvg_list.jsp").forward(request,response);
        }else if ("/insert".equals(path)) {
            String sno = request.getParameter("sno");
            String sname = request.getParameter("sname");
            Integer sage = Integer.parseInt(request.getParameter("sage"));
            String phone = request.getParameter("phone");
            String classid = request.getParameter("classid");
            dao.insert(new Student(sno, sname, sage, phone, classid));
            //重定向
            response.sendRedirect("findAll.student");
            System.out.println("插入成功");
        }else if ("/delete".equals(path)) {
            //获取sno
            String sno = request.getParameter("sno");
            dao.delete(sno);
            response.sendRedirect("findAll.student");
        } else if ("/load".equals(path)) {
            String sno = request.getParameter("sno");//获取学号
            Student student = dao.selectBySno(sno);//获取该学号的学生
            request.setAttribute("student", student);//绑定
            request.getRequestDispatcher("student_update.jsp").forward(request, response);//转发
        } else if ("/update".equals(path)) {
            String sno = request.getParameter("sno");
            Student student = dao.selectBySno(sno);
            String sname = request.getParameter("sname");
            Integer sage = Integer.parseInt(request.getParameter("sage"));
            String phone = request.getParameter("phone");
            String classid = request.getParameter("classid");
            student.setSname(sname);
            student.setSage(sage);
            student.setPhone(phone);
            student.setClassid(classid);
            dao.update(student);
            response.sendRedirect("findAll.student");
        } else if ("/findGradeBySno".equals(path)) {
//            System.out.println("通过学号找学生所有成绩");
            String sno = request.getParameter("sno");
            List<Grade> grade1 = dao1.findGradeBySno(sno);
            request.setAttribute("grade", grade1);
            request.getRequestDispatcher("grade_list.jsp").forward(request, response);
        } else if ("/loadGrade".equals(path)) {
//            System.out.println("修改成绩");
            String sno = request.getParameter("sno");
            List<Grade> grade1 = dao1.findGradeBySno(sno);
//            System.out.println(grade1);
            request.setAttribute("grade", grade1);
            request.getRequestDispatcher("grade_update.jsp").forward(request, response);
        } else if ("/updateGrade".equals(path)) {
            String[] cnos;
            String[] grade;
            String[] snames;
            String sno = request.getParameter("sno");
            String cno = request.getParameter("cno");
            cnos = request.getParameterValues("cno");
            grade = request.getParameterValues("grade");
            List<Grade> grades = dao1.findGradeBySno(sno);
            for (int i = 0; i < grades.size(); i++) {
                System.out.println(grades.get(i));
                System.out.println("=============");
                grades.get(i).setSno(request.getParameter("sno"));
                grades.get(i).setCno(cnos[i]);
                grades.get(i).setGrade(Integer.parseInt(grade[i]));
                dao1.updateGrade(grades.get(i));
                System.out.println(grades.get(i));
            }
            response.sendRedirect("findAll.student");
        } else {
            System.out.println("路径错误");
            response.sendRedirect("404.jsp");
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
